﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using YXShop.IDAL.SystemInfo;

namespace YXShop.SQLServerDAL.SystemInfo
{
    /// <summary>
    /// 数据访问类ThumbnailsSetting。
    /// </summary>
    public class ThumbnailsSetting : IThumbnailsSetting
    {
        public ThumbnailsSetting()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.SystemInfo.ThumbnailsSetting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_ThumbnailsSetting(");
            strSql.Append("ThumbnailsWidth,ThumbnailsHeight,ImageWidth,ImageHeight,Type,WatermarkPicturePath,Characters,ImgTransparent,CharTransparent,WatermarkPosition)");
            strSql.Append(" values (");
            strSql.Append("@ThumbnailsWidth,@ThumbnailsHeight,@ImageWidth,@ImageHeight,@Type,@WatermarkPicturePath,@Characters,@ImgTransparent,@CharTransparent,@WatermarkPosition)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ThumbnailsWidth", SqlDbType.VarChar,50),
					new SqlParameter("@ThumbnailsHeight", SqlDbType.VarChar,50),
					new SqlParameter("@ImageWidth", SqlDbType.VarChar,50),
					new SqlParameter("@ImageHeight", SqlDbType.VarChar,50),
					new SqlParameter("@Type", SqlDbType.VarChar,50),
					new SqlParameter("@WatermarkPicturePath", SqlDbType.VarChar,800),
					new SqlParameter("@Characters", SqlDbType.VarChar,50),
					new SqlParameter("@ImgTransparent", SqlDbType.VarChar,50),
					new SqlParameter("@CharTransparent", SqlDbType.Int,4),
					new SqlParameter("@WatermarkPosition", SqlDbType.VarChar,50)};
            parameters[0].Value = model.ThumbnailsWidth;
            parameters[1].Value = model.ThumbnailsHeight;
            parameters[2].Value = model.ImageWidth;
            parameters[3].Value = model.ImageHeight;
            parameters[4].Value = model.Type;
            parameters[5].Value = model.WatermarkPicturePath;
            parameters[6].Value = model.Characters;
            parameters[7].Value = model.ImgTransparent;
            parameters[8].Value = model.CharTransparent;
            parameters[9].Value = model.WatermarkPosition;

            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(YXShop.Model.SystemInfo.ThumbnailsSetting model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update yxs_ThumbnailsSetting set ");
            strSql.Append("ThumbnailsWidth=@ThumbnailsWidth,");
            strSql.Append("ThumbnailsHeight=@ThumbnailsHeight,");
            strSql.Append("ImageWidth=@ImageWidth,");
            strSql.Append("ImageHeight=@ImageHeight,");
            strSql.Append("Type=@Type,");
            strSql.Append("WatermarkPicturePath=@WatermarkPicturePath,");
            strSql.Append("Characters=@Characters,");
            strSql.Append("ImgTransparent=@ImgTransparent,");
            strSql.Append("CharTransparent=@CharTransparent,");
            strSql.Append("WatermarkPosition=@WatermarkPosition");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@ThumbnailsWidth", SqlDbType.VarChar,50),
					new SqlParameter("@ThumbnailsHeight", SqlDbType.VarChar,50),
					new SqlParameter("@ImageWidth", SqlDbType.VarChar,50),
					new SqlParameter("@ImageHeight", SqlDbType.VarChar,50),
					new SqlParameter("@Type", SqlDbType.VarChar,50),
					new SqlParameter("@WatermarkPicturePath", SqlDbType.VarChar,800),
					new SqlParameter("@Characters", SqlDbType.VarChar,50),
					new SqlParameter("@ImgTransparent", SqlDbType.VarChar,50),
					new SqlParameter("@CharTransparent", SqlDbType.Int,4),
					new SqlParameter("@WatermarkPosition", SqlDbType.VarChar,50)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.ThumbnailsWidth;
            parameters[2].Value = model.ThumbnailsHeight;
            parameters[3].Value = model.ImageWidth;
            parameters[4].Value = model.ImageHeight;
            parameters[5].Value = model.Type;
            parameters[6].Value = model.WatermarkPicturePath;
            parameters[7].Value = model.Characters;
            parameters[8].Value = model.ImgTransparent;
            parameters[9].Value = model.CharTransparent;
            parameters[10].Value = model.WatermarkPosition;

            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.SystemInfo.ThumbnailsSetting GetModel()
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,ThumbnailsWidth,ThumbnailsHeight,ImageWidth,ImageHeight,Type,WatermarkPicturePath,Characters,ImgTransparent,CharTransparent,WatermarkPosition from yxs_ThumbnailsSetting ");
           
            YXShop.Model.SystemInfo.ThumbnailsSetting model = new YXShop.Model.SystemInfo.ThumbnailsSetting();
            DataSet ds = ChangeHope.DataBase.SQLServerHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.ThumbnailsWidth = ds.Tables[0].Rows[0]["ThumbnailsWidth"].ToString();
                model.ThumbnailsHeight = ds.Tables[0].Rows[0]["ThumbnailsHeight"].ToString();
                model.ImageWidth = ds.Tables[0].Rows[0]["ImageWidth"].ToString();
                model.ImageHeight = ds.Tables[0].Rows[0]["ImageHeight"].ToString();
                model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                model.WatermarkPicturePath = ds.Tables[0].Rows[0]["WatermarkPicturePath"].ToString();
                model.Characters = ds.Tables[0].Rows[0]["Characters"].ToString();
                model.ImgTransparent = ds.Tables[0].Rows[0]["ImgTransparent"].ToString();
                if (ds.Tables[0].Rows[0]["CharTransparent"].ToString() != "")
                {
                    model.CharTransparent = int.Parse(ds.Tables[0].Rows[0]["CharTransparent"].ToString());
                }
                model.WatermarkPosition = ds.Tables[0].Rows[0]["WatermarkPosition"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        #endregion  成员方法
    }
}

